package com.food.mapper;

import com.food.model.UserGoal;
import com.food.model.dashboard.GoalProgress;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserGoalMapper {
    
    @Select("SELECT * FROM UserGoals WHERE user_id = #{userId}")
    UserGoal findByUserId(Integer userId);
    
    @Insert("INSERT INTO UserGoals (user_id, goal_type, target_weight, daily_calorie_target, start_date, end_date) " +
            "VALUES (#{userId}, #{goalType}, #{targetWeight}, #{dailyCalorieTarget}, #{startDate}, #{endDate})")
    void insert(UserGoal goal);
    
    @Update("UPDATE UserGoals SET goal_type = #{goalType}, target_weight = #{targetWeight}, " +
            "daily_calorie_target = #{dailyCalorieTarget}, start_date = #{startDate}, end_date = #{endDate} " +
            "WHERE user_id = #{userId}")
    void update(UserGoal goal);
    
    default void insertOrUpdate(UserGoal goal) {
        UserGoal existingGoal = findByUserId(goal.getUserId());
        if (existingGoal == null) {
            insert(goal);
        } else {
            update(goal);
        }
    }
    
    GoalProgress getGoalProgress(@Param("userId") Integer userId);
} 